﻿using FriendAD.Core.Types;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace FriendAD.web
{
    public class myCommon
    {
        public static Response saveImg(HttpPostedFileBase file)
        {
            Response _res = new Response();

            if (file == null || file.ContentLength <= 0)
            {
                _res.Message = "图片错误";
                return _res;
            }
            try
            {
                //文件目录创建
                string path = AppDomain.CurrentDomain.BaseDirectory + "uploads";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //生成文件名
                int length = file.FileName.Length;
                int lastPoint = file.FileName.LastIndexOf(".");
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + file.FileName.Substring(lastPoint, length - lastPoint);

                //保存文件
                file.SaveAs(Path.Combine(path, fileName));
                string filePath = string.Empty;
                //保存成功返回文件路径
                filePath = @"/uploads/" + fileName;
                _res.Code = 1;
                _res.Data = filePath;
            }
            catch (Exception e)
            {

                _res.Message=e.ToString();
            }         

            return _res;
        }
    }
}